package com.xykj.enterprise.wechat.ext.api.externalcontact;

import com.xykj.enterprise.wechat.bean.ext.BaseResp;
import com.xykj.enterprise.wechat.ext.service.customer.ExternalContactService;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Slf4j
@Function(description = "修改客户备注信息", parameters = {
        @Parameter(name = "access_token", type = Type.String, description = "access_token", required = true),

        @Parameter(name = "userid", type = Type.String, description = "企业成员的userid", required = true),
        @Parameter(name = "external_userid", type = Type.String, description = "外部联系人userid", required = true),
        @Parameter(name = "remark", type = Type.String, description = "此用户对外部联系人的备注，最多20个字符", required = false),
        @Parameter(name = "description", type = Type.String, description = "此用户对外部联系人的描述，最多150个字符", required = false),
        @Parameter(name = "remark_company", type = Type.String, description = "此用户对外部联系人备注的所属公司名称，最多20个字符", required = false),
        @Parameter(name = "remark_mobiles", type = Type.String, description = "此用户对外部联系人备注的手机号", required = false),
        @Parameter(name = "remark_pic_mediaid", type = Type.String, description = "备注图片的mediaid", required = false),
})
@Component
public class ExternalContactRemark implements Action {

    @Autowired
    private ExternalContactService externalContactService;

    @Override
    public Response execute(Request request) throws Exception {
        String userid = request.getString("userid");
        String external_userid = request.getString("external_userid");
        String remark = request.getString("remark");
        String description = request.getString("description");
        String remark_company = request.getString("remark_company");
        String remark_mobiles = request.getString("remark_mobiles");
        String remark_pic_mediaid = request.getString("remark_pic_mediaid");
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("userid", userid);
        params.put("external_userid", external_userid);
        if (remark != null) {
            params.put("remark", remark);
        }
        if (description != null) {
            params.put("description", description);
        }
        if (remark_company != null) {
            params.put("remark_company", remark_company);
        }
        if (remark_mobiles != null) {
            String[] mobilesSplit = remark_mobiles.split(",");
            params.put("remark_mobiles", mobilesSplit);
        }
        if (remark_pic_mediaid != null) {
            params.put("remark_pic_mediaid", remark_pic_mediaid);
        }
        BaseResp rsp = externalContactService.externalContactRemark(
                request.getString("access_token"), params
        );

        if (rsp.getErrcode() != 0) {
            return Response.fail(rsp.getErrmsg());
        }
        return Response.success();
    }
}
